package com.victualler.core.common.web;

import com.victualler.core.common.dto.list.ColumnItemDto;
import com.victualler.core.common.dto.list.DictComDto;
import com.victualler.core.common.dto.list.FormItemDto;
import com.victualler.core.common.dto.list.InitEditPageDto;
import com.victualler.core.common.dto.list.InitListPageDto;
import com.victualler.core.common.enums.ColumnFormatEnum;
import com.victualler.core.common.utils.PageInitUtil;
import com.victualler.core.config.dict.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @Aouthor Guoshichang
 * @Date 16:35 on 2018/7/14.
 * @Function
 */
@RestController
@RequestMapping("/pageInit")
public class PageInitController {

    /**字典服务类*/
    @Autowired
    private DictService dictService;

    @Value("${server.context-path:}")
    private String contextPath;

    /** 配置属性*/
    private Properties properties;

    /**初始化列表页配置信息缓存*/
    private Map<String,InitListPageDto> initListPageMap;

    /**初始化编辑页配置信息缓存*/
    private Map<String,InitEditPageDto> initEditPageMap;

    public PageInitController(){
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("pageInit.yml"));
        this.properties = yaml.getObject();

        initListPageMap = new HashMap<>();
        initEditPageMap = new HashMap<>();

    }

    /**
     * 获取初始化页面的对象
     * @return
     */
    @RequestMapping(value = "getInitListPageDto/{modelName}")
    public InitListPageDto getInitListPageDto(@PathVariable("modelName") String modelName) throws ClassNotFoundException {

        //如果在缓存中有则取缓存
        if (initListPageMap.get(modelName) != null) {
            return initListPageMap.get(modelName);
        }

        Class requestClass = Class.forName(this.properties.getProperty(modelName+".listPage.requestEntity"));
        Class responseClass = Class.forName(this.properties.getProperty(modelName+".listPage.responseEntity"));
        String editUrl =this.contextPath + this.properties.getProperty(modelName+".listPage.editUrl");
        String queryUrl =this.contextPath +  this.properties.getProperty(modelName+".listPage.queryUrl");
        InitListPageDto initListPageDto = PageInitUtil.getInitListPageDto(requestClass,responseClass);
        initListPageDto.setEditUrl(editUrl);
        initListPageDto.setQueryUrl(queryUrl);
        //加载字典
        Set<String> codeTypeSet = new HashSet<>();
        for (ColumnItemDto columnItemDto : initListPageDto.getColumnItemDtoList()) {
            if(ColumnFormatEnum.DICT.getKey().equals(columnItemDto.getType())){
                codeTypeSet.add(columnItemDto.getDictCodeType());
            }
        }
        for (FormItemDto formItemDto : initListPageDto.getQueryItemDtoList()) {
            if(ColumnFormatEnum.DICT.getKey().equals(formItemDto.getType())){
                codeTypeSet.add(formItemDto.getDictCodeType());
            }
        }
        Map<String, DictComDto> dict = dictService.getDictComByCodeType(codeTypeSet.toArray(new String[codeTypeSet.size()]));
        initListPageDto.setDict(dict);
        //加入缓存
        initListPageMap.put(modelName,initListPageDto);
        return initListPageDto;
    }

    /**
     * 获取初始化页面的对象
     * @return
     */
    @RequestMapping(value = "getInitEditPageDto/{modelName}")
    public InitEditPageDto getInitEditPageDto(@PathVariable("modelName") String modelName) throws ClassNotFoundException {

        //如果在缓存中有则取缓存
        if (initEditPageMap.get(modelName) != null) {
            return initEditPageMap.get(modelName);
        }

        Class requestClass = Class.forName(this.properties.getProperty(modelName+".editPage.entity"));
        String saveUrl =this.contextPath +  this.properties.getProperty(modelName+".editPage.saveUrl");
        String queryUrl =this.contextPath +  this.properties.getProperty(modelName+".editPage.queryUrl");
        String backUrl =this.contextPath +  this.properties.getProperty(modelName+".editPage.backUrl");
        InitEditPageDto initEditPageDto = new InitEditPageDto(PageInitUtil.getFormItemDto(requestClass));
        initEditPageDto.setSaveUrl(saveUrl);
        initEditPageDto.setQueryUrl(queryUrl);
        initEditPageDto.setBackUrl(backUrl);
        //加载字典
        Set<String> codeTypeSet = new HashSet<>();
        for (FormItemDto formItemDto : initEditPageDto.getInitSaveColumnList()) {
            if(ColumnFormatEnum.DICT.getKey().equals(formItemDto.getType())){
                codeTypeSet.add(formItemDto.getDictCodeType());
            }
        }
        Map<String, DictComDto> dict = dictService.getDictComByCodeType(codeTypeSet.toArray(new String[codeTypeSet.size()]));
        initEditPageDto.setDict(dict);

        //加入缓存
        initEditPageMap.put(modelName,initEditPageDto);
        //反馈对象
        return initEditPageDto;
    }
}
